package Forms;

import Controls.Button;
import Core.*;
import java.util.Vector;
import javax.microedition.lcdui.Font;
import MathCore.Lexeme;
import MathCore.Operands.Constant;
import MathCore.Operators.ArOperator;
import MathCore.Operators.BasicFunction;
import MathCore.Operators.TrigFunction;
import MathCore.Real;
import MathCore.Symbol;

public class AddLexeme extends Form
{
	public static final int PAGE_FUNC1=0, PAGE_FUNC2=1, PAGE_OPER=2;

	private Button button[] = new Button[9];
	private Button left, right, close;
	private Vector pages;
	
	public LexemePage activePage;
	public Lexeme res;

	public AddLexeme(Vector pages, LexemePage active)
	{
		super(Form.HEAD | Form.NO_FS | Form.NO_BG, GUICanvas.manager.getW(), 0);
		setWindowText(GM.FORM_CAPTION, "Add...");
		setWindowText(GM.PANEL_LEFTSOFT, " ");
		setWindowText(GM.PANEL_RIGHTSOFT, " ");
		setWindowText(GM.PANEL_CENTER, "");

		body.allowToEnlarge = true;
		form.allowToEnlarge = true;
		body.v_border = 3;
		body.spacing = 0;
		body.allowMovement = false;

		allowActionText = false;

		for(int i=0; i<9; i++)
		{
			button[i] = new Button("", body.getAvailableW(3), Font.getDefaultFont().getHeight()+1);
			body.setHotControl(button[i], i+1);
		}

		left = new Button("", body.getAvailableW(3), Font.getDefaultFont().getHeight()+1);
		left.addImage(new ControlImage("/contextArrow2.png", ALIGN.CENTER));
		body.setHotControl(left, 0);

		right = new Button("", body.getAvailableW(3), Font.getDefaultFont().getHeight()+1);
		right.addImage(new ControlImage("/contextArrow.png", ALIGN.CENTER));
		body.setHotControl(right, Key.POUND_KEY);

		close = new Button("", body.getAvailableW(3), Font.getDefaultFont().getHeight()+1);
		close.addImage(ControlImage.BACK_C);
		body.setHotControl(close, Key.STAR_KEY);

		body.h_border = 2;

		this.pages = pages;
		setPage(active);

		for(int i=0; i<3; i++)
		{
			LayoutManager row = new LayoutManager(body.getAvailableW(), 0);
			row.type = LayoutManager.HORIZONTAL;
			row.spacing = 0;

			for(int j=0; j<3; j++) row.addControl(button[i*3 + j]);

			body.addControl(row);
		}

		LayoutManager row = new LayoutManager(body.getAvailableW(), 0);
		row.type = LayoutManager.HORIZONTAL;
		row.spacing=0;

		row.addControl(close);
		row.addControl(left);
		row.addControl(right);

		body.addControl(row);
		
		form.updateDimensions();
		form.bg = GUICanvas.pool.CreateImage(StandartBGPath, form.getW(), form.getH(), StandartBGColor, false);
	}

	public void setPage(LexemePage page)
	{
		if(pages.indexOf(page) < 0) return;
		activePage = page;

		for(int i=0; i<page.lexemes.length; i++)
		{
			if(page.lexemes[i] != null) button[i].setCaption(page.lexemes[i].toString());
			else button[i].setCaption(" ");
		}
	}

	public void onEvent(int event, Object param)
	{
		super.onEvent(event, param);

		if(event == GM.CONTROL_RELEASED)
		{
			if(param == right)
			{
				if(pages.indexOf(activePage) == pages.size()-1) activePage = (LexemePage) pages.elementAt(0);
				else activePage = (LexemePage) pages.elementAt(pages.indexOf(activePage) + 1);
				setPage(activePage);
			}
			else if(param == left)
			{
				if(pages.indexOf(activePage) == 0) activePage = (LexemePage) pages.lastElement();
				else activePage = (LexemePage) pages.elementAt(pages.indexOf(activePage) - 1);
				setPage(activePage);
			}
			else if(param == close)
			{
				res = null;
				close();
			}
			else 
			{
				int number = 0;
				for(number=0; number<9; number++) if(button[number] == param) break;
				res = activePage.lexemes[number];
				close();
			}
		}
	}
}
